﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Admin.Master" AutoEventWireup="true" CodeBehind="Pictures.aspx.cs" Inherits="Gallery.Admin.Pictures" %>

<%@ Register
    Assembly="AjaxControlToolkit"
    Namespace="AjaxControlToolkit"
    TagPrefix="ajaxToolkit" %>


<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <script type="text/javascript">

        $(function ()
        {
            $("#documentViewDialog").dialog({ autoOpen: false, width: 640, minHeight: 480, resizable: false });
            $("#documentViewDialog").parent().appendTo(jQuery("form:first"));

            $("#addDialog").dialog({ autoOpen: false, width: 520, resizable: false });
            $("#addDialog").parent().appendTo(jQuery("form:first"));

            $(".ui-dialog-titlebar").hide();

            $("#btnCloseViewDocument").click(function ()
            {
                $("#documentViewDialog").dialog('close');
                return false;
            });
            $("#<%=btnCancel.ClientID%>").click(function ()
            {
                $('#addDialog').dialog('close');
                return false;
            });
        });

        function showImageViewDialog(url)
        {
            var wWidth = $(window).width();
            var wHeight = $(window).height();

            var dlgWidth = wWidth * 80 / 100;
            var dlgHeigth = wHeight * 80 / 100;

            var fld = $('#fieldsetDoc');
            fld.height(dlgHeigth - 50);

            var imgView = $('#imgDocument');
            var divDoc = $('#divDocView');

            divDoc.height(dlgHeigth - 100);

            imgView.attr('src', url);
            imgView.css('max-height', dlgHeigth - 120);
            imgView.show();

            var dlg = $("#documentViewDialog");
            dlg.dialog("option", "width", dlgWidth);
            dlg.dialog("option", "height", dlgHeigth);
            dlg.dialog('open');
        }

        function showAddDialog()
        {
            $("#<%=txtPictureName.ClientID%>").val('');
             $("#<%=txtYear.ClientID%>").val('');
             $("#<%=ddlAuthorAge.ClientID%>").val('-1');
             $("#<%=ddlMonth.ClientID%>").val('-1');

             var fuControl = $("#<%=fuPicture.ClientID%>");
             fuControl.replaceWith(fuControl = fuControl.clone(true));

             $('#dialogTitle').text('Add New Picture');
             $('#addDialog').dialog('open');
             return false;
        };

        function DateCreatedChanged(sender)
        {
            if (!checkdate(sender))
            {
                alert('date is invalid, please enter valid date');
               return false
            }
            return true
        }

        function checkdate(input)
        {
            var validformat = /^\d{1,2}\/\d{1,2}\/\d{4}$/ //Basic check for format validity
            var returnval = false
            if (!validformat.test(input.value))
            {
                returnval = false;
            }
            else
            {
                //Detailed check for valid date ranges
                var monthfield = input.value.split("/")[0]
                var dayfield = input.value.split("/")[1]
                var yearfield = input.value.split("/")[2]
                var dayobj = new Date(yearfield, monthfield - 1, dayfield)
                if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
                {
                    returnval = false;
                }
                else
                {
                    returnval = true
                }

            }
            if (returnval == false) input.select()
            return returnval
        }
    </script>
    <div class="top-button-panel">
        <asp:Button ID="btnAddPictureDialog" CssClass="btn apps_btn apps_blue_btn" Text="Add Picture" runat="server" OnClientClick="showAddDialog(); return false;" />
    </div>
    <asp:GridView ID="grid"
        UseAccessibleHeader="true"
        AutoGenerateColumns="false"
        runat="server"
        Width="100%"
        AllowPaging="True"
        PageSize="10000"
        OnPageIndexChanging="grid_PageIndexChanging"
        OnRowDataBound="grid_RowDataBound"
        OnRowEditing="grid_RowEditing"
        OnRowCancelingEdit="grid_RowCancelingEdit"
        OnRowUpdating="grid_RowUpdating"
        OnRowDeleting="grid_RowDeleting"
        OnRowCommand="grid_RowCommand"
        AllowSorting="true">
        <AlternatingRowStyle CssClass="grid-row grid-row-alt" />
        <RowStyle CssClass="grid-row" />
        <Columns>
            <asp:CommandField HeaderText="Edit" ShowEditButton="True">
                <ItemStyle Width="100px" HorizontalAlign="Center" />
            </asp:CommandField>
            <asp:CommandField HeaderText="Delete" ShowDeleteButton="True">
                <ItemStyle Width="100px" HorizontalAlign="Center" />
            </asp:CommandField>
            
            <asp:TemplateField HeaderText="Picture Id">
                <ItemTemplate>
                    <asp:Label ID="lblPictureId" Text='<%#Eval("PictureId") %>' Style="display: none;" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:ImageButton ID="btnUp" runat="server" CommandName="Up" CommandArgument='<%#Eval("PictureId") %>' ImageUrl="~/images/up_btn.png" />
                    <asp:ImageButton ID="btnDown" runat="server" CommandName="Down" CommandArgument='<%#Eval("PictureId") %>' ImageUrl="~/images/down_btn.png" />
                </ItemTemplate>
                <EditItemTemplate>
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Picture Name">
                <ItemTemplate>
                    <asp:Label ID="lblPictureName" Text='<%#Eval("PictureName") %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtPictureName" Text='<%#Eval("PictureName") %>' runat="server"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="File Name">
                <ItemTemplate>
                    <asp:HyperLink runat="server" Target="_blank" ID="hlViewImage" NavigateUrl="" Text='<%#Eval("FileName") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Author Age">
                <ItemTemplate>
                    <asp:Label ID="lblAuthorAge" Text='<%#Eval("AuthorAge") %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList runat="server" ID="ddlAuthorAgeGrid">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Month/Year">
                <ItemTemplate>
                    <asp:Label ID="lblDateCreated" runat="server" Text='<%#Eval("DateCreated", "{0:MM/yyyy}") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <!-- <asp:TextBox ID="txtDateCreated" onchange="return DateCreatedChanged(this);" Text='<%#Eval("DateCreated", "{0:MM/dd/yyyy}") %>' runat="server"></asp:TextBox>
                    <ajaxToolkit:CalendarExtender ID="calrndar" TargetControlID="txtDateCreated" runat="server" Format="MM/yyyy" />
                    -->
                    <asp:DropDownList runat="server" ID="ddlMonthGrid" Width="70px">
                        <asp:ListItem Text="(Month)" Value="-1" />
                        <asp:ListItem Text="Jan" Value="1" />
                        <asp:ListItem Text="Fab" Value="2" />
                        <asp:ListItem Text="Mar" Value="3" />
                        <asp:ListItem Text="Apr" Value="4" />
                        <asp:ListItem Text="May" Value="5" />
                        <asp:ListItem Text="Jun" Value="6" />
                        <asp:ListItem Text="Jul" Value="7" />
                        <asp:ListItem Text="Aug" Value="8" />
                        <asp:ListItem Text="Sep" Value="9" />
                        <asp:ListItem Text="Oct" Value="10" />
                        <asp:ListItem Text="Nov" Value="11" />
                        <asp:ListItem Text="Dec" Value="12" />
                    </asp:DropDownList>
                    &nbsp;
                    <asp:TextBox ID="txtYearGrid" ValidationGroup="Grid" runat="server"  Width="30px" />
                    <br />
                    <asp:RangeValidator ID="RangeValidator2" runat="server" Type="Integer" MinimumValue="1900" MaximumValue="2100" ControlToValidate="txtYearGrid" ErrorMessage="Value must be a whole number between 1900 and 2100" ForeColor="Red" ValidationGroup="Grid" Display="Dynamic" />
                </EditItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <div id="addDialog" class="dialog-window">
        <asp:Panel ID="Panel1" runat="server">
            <fieldset>
                <legend>
                    <h4 id="dialogTitle">Add New Picture</h4>
                </legend>

                <div>
                    <asp:HiddenField ID="hidUserId" runat="server" />
                    <label class="label_text">Picture Name</label>
                    <asp:TextBox CssClass="inputbox" ID="txtPictureName" ValidationGroup="newItem" runat="server" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtPictureName" runat="server" Text="*" ValidationGroup="newItem" ForeColor="Red" Display="Dynamic" />

                    <label class="label_text">Picture:</label>
                    <asp:FileUpload ID="fuPicture" CssClass="inputbox" runat="server" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ValidationGroup="newItem" ControlToValidate="fuPicture" Text="*" ForeColor="Red" Display="Dynamic" />

                    <label class="label_text">Author Age:</label>
                    <asp:DropDownList runat="server" ID="ddlAuthorAge" CssClass="selectbox">
                    </asp:DropDownList>

                    <label class="label_text">Month and year create:</label>
                    <asp:DropDownList runat="server" ID="ddlMonth" CssClass="selectbox">
                        <asp:ListItem Text="(Month)" Value="-1" />
                        <asp:ListItem Text="Jan" Value="1" />
                        <asp:ListItem Text="Fab" Value="2" />
                        <asp:ListItem Text="Mar" Value="3" />
                        <asp:ListItem Text="Apr" Value="4" />
                        <asp:ListItem Text="May" Value="5" />
                        <asp:ListItem Text="Jun" Value="6" />
                        <asp:ListItem Text="Jul" Value="7" />
                        <asp:ListItem Text="Aug" Value="8" />
                        <asp:ListItem Text="Sep" Value="9" />
                        <asp:ListItem Text="Oct" Value="10" />
                        <asp:ListItem Text="Nov" Value="11" />
                        <asp:ListItem Text="Dec" Value="12" />
                    </asp:DropDownList>
                    <br />
                    <asp:TextBox CssClass="inputbox" ID="txtYear" ValidationGroup="newItem" runat="server" />
                    <br />
                    <asp:RangeValidator ID="RangeValidator2" runat="server" Type="Integer" MinimumValue="1900" MaximumValue="2100" ControlToValidate="txtYear" ErrorMessage="Value must be a whole number between 1900 and 2100" ForeColor="Red" ValidationGroup="newItem" Display="Dynamic" />

                    <div>
                        <asp:Button CssClass="btn apps_btn apps_blue_btn" ID="btnSave" ValidationGroup="newItem" Text="Save" runat="server" OnClick="btnSave_Click" />
                        <asp:Button CssClass="btn apps_btn apps_blue_btn" ID="btnCancel" Text="Cancel" runat="server" />
                    </div>

                </div>
            </fieldset>
        </asp:Panel>
    </div>

    <div id="documentViewDialog" class="dialog-window">
        <fieldset id="fieldsetDoc">
            <legend>
                <h4>View image</h4>
            </legend>
            <div id="divDocView">
                <table cellpadding="0" cellspacing="0" border="0" width="100%" style="height: inherit;">
                    <tr>
                        <td align="center" valign="middle">
                            <img id="imgDocument" alt="" src="" align="middle" style="margin: 0 auto; display: block;" />
                        </td>
                    </tr>
                </table>
            </div>
            <div style="margin-top: 5px; vertical-align: bottom; height: 40px;">
                <input id="btnCloseViewDocument" type="button" title="Close" value="Close" class="btn apps_btn apps_blue_btn" />
            </div>
        </fieldset>
    </div>

</asp:Content>
